﻿
namespace EasyTest {

    /// <summary>
    /// Base class representing a Document Element to be searched for in an HTML container such as a web page, frame, or modal.
    /// </summary>
    public class IElement {

        /// <summary>
        /// String representing the Document Element to be searched for.
        /// </summary>
        public string FindBy { get; set; }

        /// <summary>
        /// Container in which to search for this Document Element.
        /// </summary>
        public Focus Focus { get; set; }

        /// <summary>
        /// Document Element that contains the Document Element being searched for.
        /// </summary>
        public IElement ParentElement { get; set; }

        /// <summary>
        /// Get the .Text property of the Document Element searched for.
        /// </summary>
        /// <returns>String representing the text contained in the .Text property.</returns>
        public string GetText() {
            GoTo();
            string text = Action.GetText(FindBy);
            return text;
        }

        /// <summary>
        /// Get the .Value property or value attribute of the Document Element searched for.
        /// </summary>
        /// <returns>String representing the text contained in the .Value property or value attribute.</returns>
        public string GetValue() {
            GoTo();
            string value = Action.GetValue(FindBy);
            return value;
        }

        /// <summary>
        /// Execute the Click() method on the Document Element found.
        /// </summary>
        public virtual void Click() {
            GoTo();
            Action.Click(FindBy);
        }
        public virtual void DoubleClick()
        {
            GoTo();
            Action.DoubleClick(FindBy);
        }

        /// <summary>
        /// Either click the ParentElement (if there is one) or load the container control.
        /// </summary>
        public void GoTo() {
            if (ParentElement != null)
                ParentElement.Click();
            if (Focus != null&Focus!=Browser.ActiveFocus)
                Focus.Go();
        }
        public virtual bool Exist()
        {
            GoTo();
            return Action.Exist(FindBy);
        }
        public virtual void VerifyExist()
        {
            VerifyExist("");
        }
        public void VerifyExist(string message)
        {
            GoTo();
            Action.Verify.Exists(FindBy,Focus,message);
        }
        public virtual void VerifyNotExist()
        {
            VerifyNotExist("");
        }
        public void VerifyNotExist(string message)
        {
            GoTo();
            Action.Verify.NotExists(FindBy, Focus, message);
        }
        public void VerifyValue(string value)
        {
            VerifyValue(value,"");
        }
        public void VerifyValue(string value, string errorMessage)
        {
            GoTo();
            Action.Verify.Value(FindBy, value, Focus, errorMessage);
        }
        public void VerifyValue(IElement element)
        {
            VerifyValue(element.GetValue(), "");
        }
        public void VerifyText(string expectedValue)
        {
            VerifyText(expectedValue, "");
        }
        public void VerifyText(string expectedValue, string errorMessage)
        {
            GoTo();
            Action.Verify.Text(FindBy, expectedValue, Focus, errorMessage);
        }
        public void VerifyEnabled()
        {
            GoTo();
            Action.Verify.Enabled(FindBy);
        }
        public void VerifyDisabled()
        {
            GoTo();
            Action.Verify.Disabled(FindBy);
        }
        public void VerifyReadOnly()
        {
            GoTo();
            Action.Verify.Readonly(FindBy);
        }
        public void WaitFor(int seconds)
        {
            Action.WaitFor(FindBy, seconds);
        }
    }

}
